/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package Controllers;

import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author BENSON
 */
public class ControllerDoctorHomepage {
     /**
     * variable, array and views declarations
     */
    Connection conn=null;
    String querry;
    Statement st=null;
    Views.DoctorHomePage docHome ; 
    ActionListener actionListener;
    Model.MainModel model;
    String [] keyArray=new String[10];
    TableRowSorter<TableModel> rowSorter;
    
    /**
     * constructor to instantiate the view
     */
    
    public ControllerDoctorHomepage( ){
        docHome = new Views.DoctorHomePage();
        docHome.setVisible(true);
        model=new Model.MainModel();
        controlDoctorCombo();
                          
    }
    
    public void showSelectedSpecialist(Object item){
        if(item.equals("GENERAL DOCTOR")){
       docHome.getDoctorTable().setRowCount(0);
        try{
        Class.forName("com.mysql.jdbc.Driver");
        String key="GENERAL DOCTOR";
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/hosiManagerDb?zeroDateTimeBehavior=convertToNull","root","");
        querry="SELECT * FROM Appointments WHERE doctor LIKE '%"+ key + "%'";
        st=conn.prepareStatement(querry);

        ResultSet rs= st.executeQuery(querry);
   while(rs.next()){
  
   Object[] temp = {rs.getString("appoinmentID"), rs.getString("patientNo"),rs.getString("patientName"),rs.getString("doctor"),
                    rs.getString("doctorReports"),rs.getString("appointmentDate")};
    docHome.getDoctorTable().addRow(temp);
    rowSorter = new TableRowSorter<>(docHome.getJTable().getModel());
    docHome.getJTable().setRowSorter(rowSorter);
    
    docHome.getSearchField().getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

        });
    
   }
   st.close();
   conn.close();
  
    }  
    catch(ClassNotFoundException | SQLException e){
    JOptionPane.showMessageDialog(null,e);
    }
   }
if(item.equals("GYNAECOLOGIST")){
       docHome.getDoctorTable().setRowCount(0);
        try{
        Class.forName("com.mysql.jdbc.Driver");
        String key="GYNAECOLOGIST";
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/hosiManagerDb?zeroDateTimeBehavior=convertToNull","root","");
        querry="SELECT * FROM Appointments WHERE doctor LIKE '%"+ key + "%'";
        st=conn.prepareStatement(querry);

        ResultSet rs= st.executeQuery(querry);
   while(rs.next()){
  
   Object[] temp = {rs.getString("appoinmentID"), rs.getString("patientNo"),rs.getString("patientName"),rs.getString("doctor"),
                    rs.getString("doctorReports"),rs.getString("appointmentDate")};
    docHome.getDoctorTable().addRow(temp);
    rowSorter = new TableRowSorter<>(docHome.getJTable().getModel());
    docHome.getJTable().setRowSorter(rowSorter);
    
    docHome.getSearchField().getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

        });
    
   }
   st.close();
   conn.close();
  
    }  
    catch(ClassNotFoundException | SQLException e){
    JOptionPane.showMessageDialog(null,e);
    }
   }
 if(item.equals("ORTHOPAEDIST")){
       docHome.getDoctorTable().setRowCount(0);
        try{
        Class.forName("com.mysql.jdbc.Driver");
        String key="ORTHOPAEDIST";
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/hosiManagerDb?zeroDateTimeBehavior=convertToNull","root","");
        querry="SELECT * FROM Appointments WHERE doctor LIKE '%"+ key + "%'";
        st=conn.prepareStatement(querry);

        ResultSet rs= st.executeQuery(querry);
   while(rs.next()){
  
   Object[] temp = {rs.getString("appoinmentID"), rs.getString("patientNo"),rs.getString("patientName"),rs.getString("doctor"),
                    rs.getString("doctorReports"),rs.getString("appointmentDate")};
    docHome.getDoctorTable().addRow(temp);
    rowSorter = new TableRowSorter<>(docHome.getJTable().getModel());
    docHome.getJTable().setRowSorter(rowSorter);
    
    docHome.getSearchField().getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

        });
    
   }
   st.close();
   conn.close();
  
    }  
    catch(ClassNotFoundException | SQLException e){
    JOptionPane.showMessageDialog(null,e);
    }
   }
   if(item.equals("PSYCHIATRIST")){
       docHome.getDoctorTable().setRowCount(0);
        try{
        Class.forName("com.mysql.jdbc.Driver");
        String key="PSYCHIATRIST";
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/hosiManagerDb?zeroDateTimeBehavior=convertToNull","root","");
        querry="SELECT * FROM Appointments WHERE doctor LIKE '%"+ key + "%'";
        st=conn.prepareStatement(querry);

        ResultSet rs= st.executeQuery(querry);
   while(rs.next()){
  
   Object[] temp = {rs.getString("appoinmentID"), rs.getString("patientNo"),rs.getString("patientName"),rs.getString("doctor"),
                    rs.getString("doctorReports"),rs.getString("appointmentDate")};
    docHome.getDoctorTable().addRow(temp);
    rowSorter = new TableRowSorter<>(docHome.getJTable().getModel());
    docHome.getJTable().setRowSorter(rowSorter);
    
    docHome.getSearchField().getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }

        });
    
   }
   st.close();
   conn.close();
  
    }  
    catch(ClassNotFoundException | SQLException e){
    JOptionPane.showMessageDialog(null,e);
    }
   }
  if(item.equals("LABORATORY")){
      docHome.getDoctorTable().setRowCount(0);
        try{
        Class.forName("com.mysql.jdbc.Driver");
        String key="LABORATORY";
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/hosiManagerDb?zeroDateTimeBehavior=convertToNull","root","");
        querry="SELECT * FROM Appointments WHERE doctor LIKE '%"+ key + "%'";
        st=conn.prepareStatement(querry);

        ResultSet rs= st.executeQuery(querry);
   while(rs.next()){
  
   Object[] temp = {rs.getString("appoinmentID"), rs.getString("patientNo"),rs.getString("patientName"),rs.getString("doctor"),
                    rs.getString("doctorReports"),rs.getString("appointmentDate")};
    docHome.getDoctorTable().addRow(temp);
    rowSorter = new TableRowSorter<>(docHome.getJTable().getModel());
    docHome.getJTable().setRowSorter(rowSorter);
    
    docHome.getSearchField().getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); 
            }

        });
    
   }
   st.close();
   conn.close();
  
    }  
    catch(ClassNotFoundException | SQLException e){
    JOptionPane.showMessageDialog(null,e);
    }
   }
   if(item.equals("DENTIST")){
      docHome.getDoctorTable().setRowCount(0);
        try{
        Class.forName("com.mysql.jdbc.Driver");
        String key="DENTIST";
        conn =(Connection)DriverManager.getConnection("jdbc:mysql://localhost:3306/hosiManagerDb?zeroDateTimeBehavior=convertToNull","root","");
        querry="SELECT * FROM Appointments WHERE doctor LIKE '%"+ key + "%'";
        st=conn.prepareStatement(querry);

        ResultSet rs= st.executeQuery(querry);
    if (!rs.next()){
        JOptionPane.showMessageDialog(null,"There are no appointments");
    }
    else{
        while(rs.next()){
  
        Object[] temp = {rs.getString("appoinmentID"), rs.getString("patientNo"),rs.getString("patientName"),rs.getString("doctor"),
                    rs.getString("doctorReports"),rs.getString("appointmentDate")};
        docHome.getDoctorTable().addRow(temp);
        rowSorter = new TableRowSorter<>(docHome.getJTable().getModel());
        docHome.getJTable().setRowSorter(rowSorter);
    
        docHome.getSearchField().getDocument().addDocumentListener(new DocumentListener(){

            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = docHome.getSearchField().getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                throw new UnsupportedOperationException("Not supported yet."); 
            }

        });
    
        }
   st.close();
   conn.close();
    }
    }
    catch(ClassNotFoundException | SQLException | HeadlessException e){
    JOptionPane.showMessageDialog(null,e);
    }
   }   
}
   
   
     String selectedNo;
     int row;
     /**
      * Adds itemListener to the combo box
      */
     
public void controlDoctorCombo(){
    docHome.getTheSpecialist().addItemListener(new ItemListener() {

        @Override
        public void itemStateChanged(ItemEvent arg0) {
            if(arg0.getStateChange() == ItemEvent.SELECTED){
                Object object = ((JComboBox)arg0.getSource()).getSelectedItem();
                showSelectedSpecialist(object);
            }
        }
    });

}
 /**
 * Method to add actionListener to the clicked buttons
 */

public void control(){        
        actionListener = new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent actionEvent) {  
                  
                  if(actionEvent.getSource()==docHome.getPullReportBtn()){
                    model.printAppointments(docHome);
                  }
                  if(actionEvent.getSource()==docHome.getLogoutBtn()){
                    ControllerMainLogin mainLog = new ControllerMainLogin();
                    docHome.setVisible(false);
                    mainLog.control();
                  }
                   if(actionEvent.getSource()==docHome.getCreateReportBtn()){
                    row = docHome.getJTable().getSelectedRow();
                    selectedNo = docHome.getDoctorTable().getValueAt(row, 0).toString();
                        if (row >= 0) {
                            keyArray[0]=selectedNo;
                        }
                    ControllerCreateReport createRepo = new ControllerCreateReport(keyArray);
                    createRepo.control();
                  }
                  
              }
              
        };
        docHome.getLogoutBtn().addActionListener(actionListener);
        docHome.getPullReportBtn().addActionListener(actionListener);
        docHome.getCreateReportBtn().addActionListener(actionListener);
}   
  
}